<?php

namespace Modules\CustomReport\Repository;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\CustomReport\Models\CustomReport;

/**
 * 自定义报表管理器
 * Class ManageRepository
 * @package Modules\CustomReport\Repository
 */
class ManageRepository
{

    /**
     * 获取视图、表字段信息
     * @param $table
     * @return array
     * @throws \Exception
     */
    static public function showTableDesc($table){
        $fields = Schema::getColumnListing($table);
        if(empty($fields)){
            throw new \Exception('视图或表名不存在');
        }

        /*$columnList = [];
        foreach ($fields as $column){
            $columnList[] = [
                'column' => $column
            ];
        }*/
        return ['table_name' => $table,'column' => $fields];

    }

    /**
     * 添加自定义报表
     * @param $postData
     * @return mixed
     */
    static public function storeBy($postData,$id = null){

        $report = [
            'report_name' => $postData['report_name'],
            'table_name' => $postData['table_name'],
            'route_path' => '/custom/report/' . $postData['table_name'],
            'fields_json' => json_encode($postData['fields_json']),
        ];
        $customReportModel = new CustomReport();
        if($id){
            return $customReportModel->where('id',$id)->update($report);
        }else{
            return $customReportModel->storeBy($report);
        }
    }

    /**
     * 根据id查询自定义报表
     * @param $id
     * @return mixed
     */
    static public function getById($id){
        return CustomReport::find($id);
    }

    /**
     * 根据报表id获取页面配置信息
     * @param $id
     * @return mixed
     */
    static public function getConfigById($id){

        $config = self::getById($id);
        if(!$config){
            throw new \Exception('自定义报表不存在');
        }

        $config = $config->toArray();
        $fields = json_decode($config['fields_json'],true);

        $title = [];
        $query = [];
        foreach ($fields as $fieldItem){
            if($fieldItem['is_show']){
                $title[] = [
                    'column'    => $fieldItem['column'],
                    'label'     => $fieldItem['label'],
                ];
            }

            if($fieldItem['is_query']){
                $query[] = [
                    'column'        => $fieldItem['column'],
                    'label'         => $fieldItem['label'],
                    'is_query'      => $fieldItem['is_query'],
                    'condition'     => $fieldItem['condition'],
                    'query_compont' => $fieldItem['query_compont'],
                ];
            }
        }
        unset($config['fields_json']);
        $config['title'] = $title;
        $config['query'] = $query;

        return $config;

    }

    /**
     * 自定义报表分页列表数据
     * @param $id
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    static public function getList($id,$params){

        $reportConfig = self::getConfigById($id);

        $limit = isset($params['limit']) ? $params['limit'] : 100;

        $builder = self::getWhere($reportConfig,$params);

        $list = $builder->paginate($limit)->toArray();

        return $list;

    }

    /**
     * 查询条件拼接
     * @param $reportConfig
     * @param $params
     * @return \Illuminate\Database\Query\Builder
     */
    static public function getWhere($reportConfig,$params){
        $builder = DB::table($reportConfig['table_name']);
        $quertConf = array_column($reportConfig['query'],null,'column');
        foreach($params as $key => $val){
            if($val != '' && isset($quertConf[$key])){
                $column = $quertConf[$key];
                if($column['condition'] == 'like'){
                    $builder = $builder->where($column['column'],$column['condition'],"%{$val}%");
                }else{
                    $builder = $builder->where($column['column'],$column['condition'],$val);
                }
            }
        }
        return $builder;
    }

}
